package customer

import (
	"context"
	dto "git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/business_opportunity"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/repository/business_opportunity"
	pb "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_user_service_proto/micro-gfyx-user-service"
	"git.myscrm.cn/golang/stark/v4"
)

type CustomerGroupService struct {
	customerGroupRepository business_opportunity.CustomerGroupRepositoryIface
}

func NewCustomerGroupService(
	customerGroupRepository business_opportunity.CustomerGroupRepositoryIface,
) CustomerGroupServiceIface {
	return &CustomerGroupService{
		customerGroupRepository: customerGroupRepository,
	}
}

func (s *CustomerGroupService) GetCustomerGroupList(ctx context.Context, request *dto.GetCustomerGroupListRequest) (*dto.GetCustomerGroupListResponse, error) {
	params := &pb.GetCustomerGroupListRequest{
		Page:       request.Page,
		PageSize:   request.PageSize,
		Status:     request.Status,
		TenantCode: request.TenantCode,
	}
	groupList, err := s.customerGroupRepository.GetCustomerGroupList(ctx, params)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetCustomerGroupList request:%v , error: %v", request, err)
		return nil, err
	}
	response := &dto.GetCustomerGroupListResponse{
		Total: groupList.Total,
		List:  make([]dto.CustomerGroupListItem, 0),
	}
	for _, group := range groupList.List {
		response.List = append(response.List, dto.CustomerGroupListItem{
			Id:          group.Id,
			Title:       group.Title,
			FileId:      group.FileId,
			IsPublic:    group.IsPublic,
			FileUrl:     group.FileUrl,
			CreatedBy:   group.CreatedBy,
			CreatedOn:   group.CreatedOn,
			Total:       group.Total,
			Status:      group.Status,
			TenantCodes: group.TenantCodes,
		})
	}

	return response, nil
}

func (s *CustomerGroupService) GetCustomerGroupDetail(ctx context.Context, request *dto.GetCustomerGroupDetailRequest) (*dto.GetCustomerGroupDetailResponse, error) {
	params := &pb.GetCustomerGroupDetailRequest{
		Id: request.Id,
	}
	detail, err := s.customerGroupRepository.GetCustomerGroupDetail(ctx, params)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetCustomerGroupDetail request:%v, error: %v", request, err)
		return nil, err
	}
	reresponse := &dto.GetCustomerGroupDetailResponse{
		Id:              detail.Id,
		Title:           detail.Title,
		IsPublic:        detail.IsPublic,
		LandUsage:       detail.LandUsage,
		LandUsageOp:     detail.LandUsageOp,
		LandTitle:       detail.LandTitle,
		LandTitleOp:     detail.LandTitleOp,
		BuildingType:    detail.BuildingType,
		BuildingTypeOp:  detail.BuildingTypeOp,
		BuildingTitle:   detail.BuildingTitle,
		BuildingTitleOp: detail.BuildingTitleOp,
		BidTitle:        detail.BidTitle,
		BidTitleOp:      detail.BidTitleOp,
		WinbidTitle:     detail.WinbidTitle,
		WinbidTitleOp:   detail.WinbidTitleOp,
		FileId:          detail.FileId,
		FileUrl:         detail.FileUrl,
		TenantCodes:     detail.TenantCodes,
	}

	return reresponse, nil
}

func (s *CustomerGroupService) SaveCustomerGroup(ctx context.Context, request *dto.SaveCustomerGroupRequest) (*dto.SaveCustomerGroupResponse, error) {
	params := &pb.SaveCustomerGroupRequest{
		Title:           request.Title,
		LandTitle:       request.LandTitle,
		IsPublic:        request.IsPublic,
		LandTitleOp:     request.LandTitleOp,
		LandUsage:       request.LandUsage,
		LandUsageOp:     request.LandUsageOp,
		BuildingTitle:   request.BuildingTitle,
		BuildingTitleOp: request.BuildingTitleOp,
		BuildingType:    request.BuildingType,
		BuildingTypeOp:  request.BuildingTypeOp,
		BidTitle:        request.BidTitle,
		BidTitleOp:      request.BidTitleOp,
		WinbidTitle:     request.WinbidTitle,
		WinbidTitleOp:   request.WinbidTitleOp,
		FileId:          request.FileId,
		TenantCodes:     request.TenantCodes,
		CreatedBy:       request.CreatedBy,
	}
	if request.Id != 0 {
		params.Id = request.Id
	}
	result, err := s.customerGroupRepository.SaveCustomerGroup(ctx, params)
	if err != nil {
		stark.Logger.Errorf(ctx, "SaveCustomerGroup request:%v, error: %v", request, err)
		return nil, err
	}

	return &dto.SaveCustomerGroupResponse{Id: result.Id}, nil
}

func (s *CustomerGroupService) DeleteCustomerGroup(ctx context.Context, request *dto.DeleteCustomerGroupRequest) (*dto.DeleteCustomerGroupResponse, error) {
	params := &pb.DeleteCustomerGroupRequest{
		Id: request.Id,
	}
	result, err := s.customerGroupRepository.DeleteCustomerGroup(ctx, params)
	if err != nil {
		stark.Logger.Errorf(ctx, "DeleteCustomerGroup request:%v, error: %v", request, err)
		return nil, err
	}
	return &dto.DeleteCustomerGroupResponse{AffectedRows: result.AffectRows}, nil
}
